<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\property\basic;

use app\common\dao\BaseDao;
use app\common\model\property\basic\PropertyLabelModel;

class PropertyLabelDao extends BaseDao
{

    /**
     * @return PropertyLabelModel
     */
    protected function getModel(): string
    {
        return PropertyLabelModel::class;
    }


    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['is_show']) && $where['is_show'] !== '', function ($query) use ($where) {
                $query->where('is_show', intval($where['is_show']));
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('label_name|remark', '%' . trim($where['keywords']) . '%');
            });
    }

    /**
     * 根据条件查全部
     * @param $propertyId
     * @param $isShow
     * @param $field
     * @return array|\think\Collection|\think\db\BaseQuery[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-12 11:21
     */
    public function getAll($propertyId = null, $isShow = '', $field = '*')
    {
        $query = $this->search([
            'is_show' => $isShow,
        ], $propertyId);
        return $query->field($field)->order('sort DESC,' . $this->getPk() . ' DESC')->select();
    }

}
